package com.esiran.payadmin.controller;

import com.esiran.payadmin.entity.Resp;
import com.esiran.payadmin.exception.APIRespException;
import com.esiran.payadmin.model.Order;
import com.esiran.payadmin.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/order")
public class APIOrderController {
    private final OrderService orderService;
    @Autowired
    public APIOrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/state")
    @ResponseBody
    public Resp<Map<String,Object>> orderState(
            @RequestParam("order_num") String num) throws APIRespException {
        Order order =  orderService.getOrderByNum(num);
        if (order == null){
            throw new APIRespException("订单查询失败",-1000);
        }
        Map<String,Object> resp = new HashMap<>();
        resp.put("orderNum",order.getNum());
        resp.put("state",order.getState());
        resp.put("order_num",order.getNum());
        resp.put("extra_num",order.getExtraId());
        resp.put("amount",order.getAmount());
        return Resp.create(resp);
    }
}
